
package com.fan89.config;

import com.fan89.core.interceptor.BackInterceptor;
import com.fan89.core.interceptor.TimeCostInterceptor;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * 后台Web配置类
 */
@Configuration
public class CustomSpringMvcConfig extends WebMvcConfigurerAdapter {

    public CustomSpringMvcConfig() {
        System.err.println("BackendWebConfig");
    }

    // /**
    // * 注册{@link BindingInitializer}和{@link PageableArgumentResolver}
    // */
    // @Bean
    // public RequestMappingHandlerAdapter requestMappingHandlerAdapter(
    // @Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager,
    // @Qualifier("mvcConversionService") FormattingConversionService conversionService,
    // @Qualifier("mvcValidator") Validator validator) {
    // RequestMappingHandlerAdapter adapter = super.requestMappingHandlerAdapter(contentNegotiationManager,
    // conversionService, validator);
    // adapter.setWebBindingInitializer(new BindingInitializer());
    // List<HandlerMethodArgumentResolver> argumentResolvers = new ArrayList<HandlerMethodArgumentResolver>();
    // argumentResolvers.add(new PageableArgumentResolver());
    // adapter.setCustomArgumentResolvers(argumentResolvers);
    // return adapter;
    // }

    /**
     * 后台拦截器
     *
     * @return
     */
    @Bean
    public BackInterceptor backInterceptor() {
        return new BackInterceptor();
    }

    /**
     * 注册拦截器。包括执行计时拦截器和后台拦截器。
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new TimeCostInterceptor()).addPathPatterns("/*").excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**", "/doc.html/**");
//        registry.addInterceptor(backInterceptor()).addPathPatterns("/*").excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**", "/doc.html/**");
        registry.addInterceptor(new TimeCostInterceptor()).excludePathPatterns("/ji");
        registry.addInterceptor(backInterceptor()).addPathPatterns("/app/**");
        super.addInterceptors(registry);
    }

    // hibernate validation
    @Bean
    public Validator getValidator() {
        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
        bean.setValidationMessageSource(messageSource());
        bean.setProviderClass(org.hibernate.validator.HibernateValidator.class);
        return bean;
    }

    // <!-- 消息国际化 -->
    public MessageSource messageSource() {
        ResourceBundleMessageSource bean = new ResourceBundleMessageSource();
        bean.setBasenames("errormsg", "validation_error");
        return bean;
    }

    @Bean
    public Module jsonMapperJava8DateTimeModule() {
        SimpleModule module = new SimpleModule();
        module.addSerializer(LocalDateTime.class,
                new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        module.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        module.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));

        return module;
    }
}
